#ifndef _FSIP1P_H_
#define _FSIP1P_H_

#include "Equations/TwoPhaseEquation.hpp"

namespace Tuna {

  template <typename T, int Dim> class FSIP1P;

  template <typename T>
  struct Typeinfo <FSIP1P<T, 1> > {
    typedef T prec_t;
    enum { Dim = 1 };
  };

  template <typename T>
  struct Typeinfo <FSIP1P<T, 2> > {
    typedef T prec_t;
    enum { Dim = 2 };
  };

  template <typename T>
  struct Typeinfo <FSIP1P<T, 3> > {
    typedef T prec_t;
    enum { Dim = 3 };
  };

  
  template<typename Tprec, int Dim>
  class FSIP1P : public TwoPhaseEquation<FSIP1P<Tprec, Dim> >
  {

    typedef TwoPhaseEquation<FSIP1P<Tprec, Dim> > TP_FSIP1P;
    
    using GeneralEquation< TP_FSIP1P >::aE;
    using GeneralEquation< TP_FSIP1P >::aW;
    using GeneralEquation< TP_FSIP1P >::aN;
    using GeneralEquation< TP_FSIP1P >::aS;
    using GeneralEquation< TP_FSIP1P >::aF;
    using GeneralEquation< TP_FSIP1P >::aB;
    using GeneralEquation< TP_FSIP1P >::aP;
    using GeneralEquation< TP_FSIP1P >::sp;
    using GeneralEquation< TP_FSIP1P >::dx;
    using GeneralEquation< TP_FSIP1P >::dy;
    using GeneralEquation< TP_FSIP1P >::dz;
    using GeneralEquation< TP_FSIP1P >::bi;
    using GeneralEquation< TP_FSIP1P >::ei;
    using GeneralEquation< TP_FSIP1P >::bj;
    using GeneralEquation< TP_FSIP1P >::ej;
    using GeneralEquation< TP_FSIP1P >::bk;
    using GeneralEquation< TP_FSIP1P >::ek;
    using GeneralEquation< TP_FSIP1P >::applyBoundaryConditions1D;
    using GeneralEquation< TP_FSIP1P >::applyBoundaryConditions2D;
    using GeneralEquation< TP_FSIP1P >::applyBoundaryConditions3D;

    using TP_FSIP1P::S;
    using TP_FSIP1P::phi_0;
    using TP_FSIP1P::Srw;
    using TP_FSIP1P::Sro;
    using TP_FSIP1P::mu_w;
    using TP_FSIP1P::mu_o;
    using TP_FSIP1P::k;
    using TP_FSIP1P::rank;
    using TP_FSIP1P::size;
    using TP_FSIP1P::injection;

  public:
    typedef Tprec prec_t;
    typedef typename TunaArray<prec_t, Dim >::huge ScalarField;
    
    FSIP1P() : TwoPhaseEquation<FSIP1P<prec_t, Dim > >() { }    
    ~FSIP1P() { };
    
    inline bool calcCoefficients1D(); 
    inline bool calcCoefficients2D();
    inline bool calcCoefficients3D();
    inline void printInfo() { std::cout << " FSIP1P "; }
  };
  
/*
 *  Lineal for realtive permeability , Upwind for Sw
 */
template<typename Tprec, int Dim>
inline bool FSIP1P<Tprec, Dim>::calcCoefficients1D () 
{
    static prec_t Sw_e, Sw_w;

    // Lineal 
    static prec_t mult_o = k / ( (1 - Srw - Sro) * mu_o * dx ) ;
    static prec_t mult_w = k / ( (1 - Srw - Sro) * mu_w * dx ) ;

    aE = 0.0; aW = 0.0; aP = 0.0; sp = 0.0;

    for (int i =  bi; i <= ei; ++i) {  

      // Upwind
      if ( phi_0(i+1) >= phi_0(i) ) Sw_e = S(i+1);
      else                          Sw_e = S(i);
      if ( phi_0(i-1) >= phi_0(i) ) Sw_w = S(i-1);
      else                          Sw_w = S(i);
      
      // Lineal
      aE (i) = (1 - Sro - Sw_e) * mult_o + (Sw_e - Srw) * mult_w ;
      aW (i) = (1 - Sro - Sw_w) * mult_o + (Sw_w - Srw) * mult_w ;
      aP (i) = aE (i) + aW (i);      
    }
    applyBoundaryConditions1D();
    return 0;
}

/*
 *  Lineal for relative permeability , Upwind for Sw
 */
template<typename Tprec, int Dim>
inline bool FSIP1P<Tprec, Dim>::calcCoefficients2D () 
{
  static prec_t Sw_e, Sw_w, Sw_n, Sw_s;

    // Lineal 
    static prec_t mult_o = k / ( (1 - Srw - Sro) * mu_o ) ;
    static prec_t mult_w = k / ( (1 - Srw - Sro) * mu_w ) ;
    static prec_t dx_dy = dx / dy;
    static prec_t dy_dx = dy / dx;

    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aP = 0.0; sp = 0.0;


    for (int i =  bi; i <= ei; ++i)
      for (int j = bj; j <= ej; ++j) {  
	// Upwind
	if ( phi_0(i+1, j) >= phi_0(i, j) ) Sw_e = S(i+1, j);
	else                                Sw_e = S(i,   j);
	if ( phi_0(i-1, j) >= phi_0(i, j) ) Sw_w = S(i-1, j);
	else                                Sw_w = S(i,   j);

	if ( phi_0(i, j+1) >= phi_0(i, j) ) Sw_n = S(i, j+1);
	else                                Sw_n = S(i,   j);
	if ( phi_0(i, j-1) >= phi_0(i, j) ) Sw_s = S(i, j-1);
	else                                Sw_s = S(i,   j);
      
      // Lineal
	aE (i, j) = ( (1 - Sro - Sw_e) * mult_o +
		      (Sw_e - Srw) * mult_w ) * dy_dx;
	aW (i, j) = ( (1 - Sro - Sw_w) * mult_o + 
		      (Sw_w - Srw) * mult_w ) * dy_dx;
	aN (i, j) = ( (1 - Sro - Sw_n) * mult_o + 
		      (Sw_n - Srw) * mult_w ) * dx_dy;
	aS (i, j) = ( (1 - Sro - Sw_s) * mult_o + 
		      (Sw_s - Srw) * mult_w ) * dx_dy;
	aP (i, j) = aE (i, j) + aW (i, j) + aN (i, j) + aS (i, j);

      }
    
    // Source for injection on the four corners.
    if (rank == 0)
      sp (bi, bj) += injection * dx * dy;
  
    //Sink for extraction on the middle point.
    if (rank == size-1)
      sp (ei, ej) -= injection * dx * dy;
   
    applyBoundaryConditions2D();
    return 0;
}

/*
 *  Lineal for relative permeability , Upwind for Sw
 */
template<typename Tprec, int Dim>
inline bool FSIP1P<Tprec, Dim>::calcCoefficients3D () 
{
    static prec_t Sw_e, Sw_w, Sw_n, Sw_s, Sw_f, Sw_b;

    // Lineal 
    static prec_t mult_o = k / ( (1 - Srw - Sro) * mu_o ) ;
    static prec_t mult_w = k / ( (1 - Srw - Sro) * mu_w ) ;
    static prec_t dxdz_dy = dx * dz / dy;
    static prec_t dydz_dx = dy * dz / dx;
    static prec_t dxdy_dz = dx * dy / dz;

    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aF = 0.0; aB = 0.0;
    aP = 0.0; sp = 0.0;

    for (int ki = bk; ki <= ek; ++ki) 
      for (int i =  bi; i <= ei; ++i) 
	for (int j = bj; j <= ej; ++j) {
	  // Upwind
	  if ( phi_0(i+1, j, ki) >= phi_0(i, j, ki) ) Sw_e = S(i+1, j, ki);
	  else                                        Sw_e = S(i,   j, ki);
	  if ( phi_0(i-1, j, ki) >= phi_0(i, j, ki) ) Sw_w = S(i-1, j, ki);
	  else                                        Sw_w = S(i,   j, ki);
	  
	  if ( phi_0(i, j+1, ki) >= phi_0(i, j, ki) ) Sw_n = S(i, j+1, ki);
	  else                                        Sw_n = S(i,   j, ki);
	  if ( phi_0(i, j-1, ki) >= phi_0(i, j, ki) ) Sw_s = S(i, j-1, ki);
	  else                                        Sw_s = S(i,   j, ki);

	  if ( phi_0(i, j, ki+1) >= phi_0(i, j, ki) ) Sw_f = S(i, j, ki+1);
	  else                                        Sw_f = S(i, j, ki);
	  if ( phi_0(i, j, ki-1) >= phi_0(i, j, ki) ) Sw_b = S(i, j, ki-1);
	  else                                        Sw_b = S(i, j, ki);
	  
	  // Lineal
	  aE (i, j, ki) = ( (1 - Sro - Sw_e) * mult_o + 
			   (Sw_e - Srw) * mult_w ) * dydz_dx;
	  aW (i, j, ki) = ( (1 - Sro - Sw_w) * mult_o + 
			   (Sw_w - Srw) * mult_w ) * dydz_dx;
	  aN (i, j, ki) = ( (1 - Sro - Sw_n) * mult_o + 
			   (Sw_n - Srw) * mult_w ) * dxdz_dy;
	  aS (i, j, ki) = ( (1 - Sro - Sw_s) * mult_o + 
			   (Sw_s - Srw) * mult_w ) * dxdz_dy;
	  aF (i, j, ki) = ( (1 - Sro - Sw_f) * mult_o + 
			   (Sw_f - Srw) * mult_w ) * dxdy_dz;
	  aB (i, j, ki) = ( (1 - Sro - Sw_b) * mult_o + 
			   (Sw_b - Srw) * mult_w ) * dxdy_dz;	  
	  aP (i, j, ki) = aE (i, j, ki) + aW (i, j, ki) + 
	                  aN (i, j, ki) + aS (i, j, ki) + 
	                  aF (i, j, ki) + aB (i, j, ki);
	}        
    applyBoundaryConditions3D();
    return 0;
}

} // Tuna namespace


#endif //_FSIP1P_H_

















